package com.liehu.business.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.liehu.business.dao.LeavemessageMapper;
import com.liehu.business.pojo.LeaveMessage;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Transactional//事务

public class LeavemessageService {
    @Autowired
    private LeavemessageMapper leavemessageMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private HttpServletRequest request;

    /**
     * 查询所有
     */
    public List<LeaveMessage> findAll(){
        return leavemessageMapper.selectList(null);
    }
    /**
     * 根据id查询
     */
    public LeaveMessage findById(String id){
        return leavemessageMapper.selectById(id);
    }

    /**
     * 插入
     */
    public void save(LeaveMessage leavemessage){
        Claims claims_customer = (Claims) request.getAttribute("claims_customer");
        if(claims_customer == null ){
            throw new RuntimeException("权限不足！");
        }
        //给这条记录插入id
        leavemessage.setId(idWorker.nextId()+"");
        //设置状态为待审核
        leavemessage.setStatus("0");
        leavemessageMapper.insert(leavemessage);
    }

    /**
     * 更新
     */
    public void update(LeaveMessage leavemessage){
        Claims claims_customer = (Claims) request.getAttribute("claims_customer");
        if(claims_customer == null ){
            throw new RuntimeException("权限不足！");
        }
        leavemessageMapper.updateById(leavemessage);
    }
    /**
     * 根据id删除
     */
    public void deleteById(String id){
        Claims claims_admin = (Claims) request.getAttribute("claims_admin");
        if(claims_admin == null ){
            throw new RuntimeException("权限不足！");
        }
        leavemessageMapper.deleteById(id);
    }
    /**
     * 分页查询
     */
    public Page<LeaveMessage> findByPage(Integer page, Integer size){
        Page<LeaveMessage> pageable = new Page<>(page,size);//创建分页对象
        return  leavemessageMapper.selectPage(pageable, null);
    }
    /**
     * 条件分页查询
     */
    public Page<LeaveMessage> findByPage(LeaveMessage leavemessage, Integer page, Integer size){
        Page<LeaveMessage> pageable = new Page<>(page,size);//创建分页对象
        QueryWrapper<LeaveMessage> wrapper = myWrapper(leavemessage);//创建条件对象wrapper
        return  leavemessageMapper.selectPage(pageable, wrapper);
    }

    /**
     * 内部处理条件
     * @param leavemessage
     * @return
     */
    private QueryWrapper<LeaveMessage> myWrapper(LeaveMessage leavemessage){
        QueryWrapper<LeaveMessage> wrapper = new QueryWrapper<>();
        //条件构造1
        if(leavemessage.getContent()!=null && !leavemessage.getContent().equals("")){
            wrapper.like("content","%"+leavemessage.getContent()+"%");
        }
        //条件构造2
        if(leavemessage.getCustomerName()!=null&& !leavemessage.getCustomerName().equals("")){
            wrapper.like("customer_name","%"+leavemessage.getCustomerName()+"%");
        }
        //条件构造3
        if(leavemessage.getId()!=null&&!leavemessage.getId().equals("")){
            wrapper.eq("id",leavemessage.getId());
        }

        return  wrapper;
    }
    /**
    * 审核留言-仅限管理员操作-leaveMessageId为留言id,
    * status为留言状态，0为待审核，1为审核通过
    **/
    public void updateStatus(String leaveMessageId, String status) {
        Claims claims_admin = (Claims) request.getAttribute("claims_admin");
        if(claims_admin == null ){
            throw new RuntimeException("权限不足！");
        }
        leavemessageMapper.updateStatus(leaveMessageId,status);


    }
}
